package io.github.talelin.latticy.controller.v1;


import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.model.AswTypeDO;
import io.github.talelin.latticy.service.AswTypeService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Map;

/**
 * 题目分类表前端控制器
 *
* @author generator@tomingto
* @since 2021-06-02
*/
@RestController
@RequestMapping("/v1/type")
public class AswTypeController {

    @Autowired
    private AswTypeService aswTypeService;

    @GetMapping("/{id}")
    public AswTypeDO getType(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        AswTypeDO aswTypeDO = aswTypeService.getById(id);
        if (aswTypeDO == null) {
            throw new NotFoundException(10022);
        }
        return aswTypeDO;
    }

    @GetMapping("")
    public List<AswTypeDO> getTypes() {
        List<AswTypeDO> aswTypes = aswTypeService.findAll();
        return aswTypes;
    }

    @PostMapping("/id")
    public boolean updateTypeState(@RequestBody Map<String,Object> map) {
        return aswTypeService.updateTypeState(Integer.parseInt(map.get("tempStatus").toString()),Integer.parseInt(map.get("id").toString()))>0;
    }

    @PutMapping("/{id}")
    @PermissionMeta(value = "修改分类", module = "题目分类")
    public UpdatedVO updateType(@PathVariable("id") Integer id, @RequestBody Map<String,Object> map) {
        AswTypeDO aswTypeDO = aswTypeService.getById(id);
        if (aswTypeDO == null) {
            throw new NotFoundException(10022);
        }
        aswTypeService.updateType(aswTypeDO, map);
        return new UpdatedVO(17);
    }
    @PostMapping("")
    public CreatedVO createAsw(@RequestBody  Map<String,Object> map) {
        aswTypeService.createType(map);
        return new CreatedVO(16);
    }


    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除分类", module = "题目分类")
    public DeletedVO deleteBook(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
        AswTypeDO typeDO = aswTypeService.getById(id);
        if (typeDO == null) {
            throw new NotFoundException(10022);
        }
        aswTypeService.deleteById(typeDO.getId());
        return new DeletedVO(18);
    }

}
